package com.milhouz.bomberrokon.sprite;

import com.milhouz.bomberrokon.BomberSlashActivity;
import com.milhouz.bomberrokon.texture.TextureLoader;
import com.stickycoding.Rokon.Rokon;
import com.stickycoding.Rokon.Sprite;
import com.stickycoding.Rokon.Texture;

/**
 * HUD Sprite components
 */
public class HudSprite {

	public static final int HUD_HEIGHT = 32;
	public static final int THUMB_SIZE = 16;

	/** Hud rokon layer index */
	private int mHudLayer;
	/** Hud background sprite */
	private Sprite mBackgroundSprite;
	/** Hud timer sprite */
	private Sprite mTimerSprite;
	/** Bomber thumb sprites */
	private Sprite[] mBomberThumbSprites;
	/** Bomber victory count sprites */
	private Sprite[] mBomberVictoryCountSprites;
	/** Timer sprites */
	private Sprite[] mTimerSprites;
	/** Nb players */
	private int mNbPlayers;
	/** Game victories */
	private int[] mGameVictories;

	public HudSprite(int hudLayer, int nbPlayers, Texture[] bomberTextures, int[] gameVictories) {
		mHudLayer = hudLayer;
		mNbPlayers = nbPlayers;
		mGameVictories = gameVictories;
		mBomberThumbSprites = new Sprite[nbPlayers];
		mBomberVictoryCountSprites = new Sprite[nbPlayers];
		mBackgroundSprite = new Sprite(0, 0, TextureLoader.getInstance().getHudTextures()[0]);
		centerSprites();
		initTimer();
		initIndicators(bomberTextures);
		Rokon.getRokon().addSprite(mBackgroundSprite, hudLayer);
	}

	/** HUD loop update */
	public void loop(int[] timerArray) {
		// Update timer text
		mTimerSprites[0].setTileIndex(timerArray[0] + 1);
		mTimerSprites[1].setTileIndex(timerArray[1] + 1);
		mTimerSprites[2].setTileIndex(timerArray[2] + 1);
	}

	/** Init HUD Sprites */
	private void initIndicators(Texture[] bomberTextures) {
		// For each bombers
		for (int i = 0; i < mGameVictories.length; i++) {
			mBomberThumbSprites[i] = new Sprite(getNextTabX(i, true), mYPosition, THUMB_SIZE, THUMB_SIZE, bomberTextures[i]);
			Rokon.getRokon().addSprite(mBomberThumbSprites[i], mHudLayer);
			mBomberVictoryCountSprites[i] = new Sprite(getNextTabX(i, false), mYPosition, TextureLoader.getInstance().getHudTextures()[1]);
			Rokon.getRokon().addSprite(mBomberVictoryCountSprites[i], mHudLayer);
			mBomberVictoryCountSprites[i].setTileIndex(mGameVictories[i] + 1);
		}
	}

	/** Init Timer sprites */
	private void initTimer() {
		mTimerSprite = new Sprite(15, 1, TextureLoader.getInstance().getHudTextures()[2]);
		Rokon.getRokon().addSprite(mTimerSprite, mHudLayer);
		mTimerSprites = new Sprite[3];
		mTimerSprites[0] = new Sprite(45, 5, TextureLoader.getInstance().getHudTextures()[1]);
		mTimerSprites[0].setTileIndex(1);
		Rokon.getRokon().addSprite(mTimerSprites[0], mHudLayer);
		mTimerSprites[1] = new Sprite(60, 5, TextureLoader.getInstance().getHudTextures()[1]);
		mTimerSprites[1].setTileIndex(1);
		Rokon.getRokon().addSprite(mTimerSprites[1], mHudLayer);
		mTimerSprites[2] = new Sprite(75, 5, TextureLoader.getInstance().getHudTextures()[1]);
		mTimerSprites[2].setTileIndex(1);
		Rokon.getRokon().addSprite(mTimerSprites[2], mHudLayer);
	}

	private int mXPosition = 0;
	private int mYPosition = 0;
	private final int POSITION_Y = 20;
	private final int POSITION_X = 50;
	private final int POSITION_X2 = 20;

	private void centerSprites() {
		mXPosition = ((BomberSlashActivity.SCREEN_WIDTH - (mNbPlayers * (POSITION_X + POSITION_X2))) / 2);
		mYPosition = ((HUD_HEIGHT - POSITION_Y) / 2);
	}

	private int getNextTabX(int i, boolean thumb) {
		if (thumb && i != 0) {
			mXPosition += POSITION_X;
		} else {
			mXPosition += POSITION_X2;
		}
		return mXPosition;
	}

}
